package cn.stylefeng.guns.modular.ry600.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.stylefeng.roses.core.base.controller.BaseController;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;

import cn.stylefeng.guns.core.log.LogObjectHolder;

import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.stylefeng.guns.modular.system.model.Clientcustomer;
import cn.stylefeng.guns.modular.system.model.Clientproduct;
import cn.stylefeng.guns.modular.system.model.ErpProduct;
import cn.stylefeng.guns.modular.system.model.Erpkind;
import cn.stylefeng.guns.modular.ry600.service.IClientproductService;
import cn.stylefeng.guns.modular.ry600.service.IErpkindService;

/**
 * 连锁商品管理控制器
 *
 * @author fengshuonan
 * @Date 2018-12-13 20:40:54
 */
@Controller
@RequestMapping("/clientproduct")
public class ClientproductController extends BaseController {

    private String PREFIX = "/ry600/clientproduct/";

    @Autowired
    private IClientproductService clientproductService;
    @Autowired
	private IErpkindService erpkindService;

    /**
     * 跳转到连锁商品管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "clientproduct.html";
    }

    /**
     * 跳转到添加连锁商品管理
     */
    @RequestMapping("/clientproduct_add")
    public String clientproductAdd() {
        return PREFIX + "clientproduct_add.html";
    }

    /**
     * 跳转到修改连锁商品管理
     */
    @RequestMapping("/clientproduct_update/{clientproductId}")
    public String clientproductUpdate(@PathVariable Integer clientproductId, Model model) {
        Clientproduct clientproduct = clientproductService.selectById(clientproductId);
        model.addAttribute("item",clientproduct);
        LogObjectHolder.me().set(clientproduct);
        return PREFIX + "clientproduct_edit.html";
    }

    /**
     * 获取连锁商品管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return clientproductService.selectList(null);
    }

    /**
     * 新增连锁商品管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Clientproduct clientproduct) {
        clientproductService.insert(clientproduct);
        return SUCCESS_TIP;
    }

    /**
     * 删除连锁商品管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer clientproductId) {
        clientproductService.deleteById(clientproductId);
        return SUCCESS_TIP;
    }

    /**
     * 修改连锁商品管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Clientproduct clientproduct) {
        clientproductService.updateById(clientproduct);
        return SUCCESS_TIP;
    }

    /**
     * 连锁商品管理详情
     */
    @RequestMapping(value = "/detail/{clientproductId}")
    @ResponseBody
    public Object detail(@PathVariable("clientproductId") Integer clientproductId) {
        return clientproductService.selectById(clientproductId);
    }
    
    
    @RequestMapping(value = "/selectByMsg")
    @ResponseBody
    public Object selectByMsg(HttpServletRequest request, HttpServletResponse response) {
    	Integer page = Integer.valueOf(request.getParameter("page"));
		Integer rows = Integer.valueOf(request.getParameter("rows"));
    	Map<String, Object> msg=this.setMap(request);
    	PageHelper.startPage(page, rows);
    	List<Clientproduct> result=(List<Clientproduct>) clientproductService.selectByMsg(msg);
    	PageInfo<Clientproduct> pageInfo = new PageInfo<Clientproduct>(result);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return map;
        
    }
    
    public Map<String,Object> setMap(HttpServletRequest request){
    	Map<String, Object> msg = new HashMap<String, Object>();
    	String searchKey = request.getParameter("searchKey");
		String searchWord = request.getParameter("searchWord");
		String searchKey1 = request.getParameter("searchKey1");
		String searchWord1 = request.getParameter("searchWord1");
		
		if (!StringUtils.isBlank(searchKey1) && !StringUtils.isBlank(searchWord1)) {
			msg.put(searchKey1.trim(), searchWord1.trim());
		}
		if (!StringUtils.isBlank(searchKey) && !StringUtils.isBlank(searchWord)) {
			msg.put(searchKey.trim(), searchWord.trim());
		}
		return msg;
    }
    
    @RequestMapping(value = "/batchadd")
    @ResponseBody
    public Object batchadd(String  json) {
//    	List<Clientcustomer>  clientcustomers=(List<Clientcustomer>) JSON.parse(json);
    	List<Clientproduct> clientproducts=JSON.parseArray(json, Clientproduct.class);
    	List<Erpkind> resultErpKind = erpkindService.selectList(null);
		Set<String> dataSource = new HashSet<String>();
		for (int j = 0; j < resultErpKind.size(); j++) {
			if (dataSource.add(resultErpKind.get(j).getDataSource())) {
				clientproductService.insertBatch(clientproducts, resultErpKind.get(j).getDataSource());
			}}
        return SUCCESS_TIP;
    }
    
    @RequestMapping(value = "/delByDao")
    @ResponseBody
    public Object delByDao(@RequestParam(required=false) String clientId,
    		@RequestParam(required=false) String productId){
    	Clientproduct clientproduct=new Clientproduct();
    	clientproduct.setClientId(clientId);
    	clientproduct.setProductId(productId);
    	List<Erpkind> resultErpKind = erpkindService.selectList(null);
		Set<String> dataSource = new HashSet<String>();
		for (int j = 0; j < resultErpKind.size(); j++) {
			if (dataSource.add(resultErpKind.get(j).getDataSource())) {
				clientproductService.delByDao(clientproduct, resultErpKind.get(j).getDataSource());
				
			}
		}
    	return SUCCESS_TIP;
    }
}
